/*
* @file investmentAnalysis/AnalysisReport.js
* 分析报告
*/


import React, { Component, PropTypes } from 'react';
import { Row, Col } from 'antd';
import { connect } from 'react-redux';
import moment from 'moment';
import _ from 'lodash';
import 'github-markdown-css';
import ReactMarkdown from 'react-markdown';
import OrderDetail from '../OrderDetail';
import Comments from './comments';
import utils from '../utils';

const mapStateToProps = ({
	featureList: { currentDataDate, selectedTrader },
	accountAssets: { specialTradings, commentConfig, featureRanks },
}) => ({
	detail: selectedTrader,
	dataDate: moment(currentDataDate).format('YYYY-MM-DD'),
	specialTradings,
	commentConfig,
	featureRanks,
});

const mapDispatchToProps = {
	getSpecialTradings: query => ({
		type: 'accountAssets/getSpecialTradings',
		payload: query || {},
	}),
	getCommentConfig: query => ({
		type: 'accountAssets/getCommentConfig',
		payload: query || {},
	}),
	getFeatureRanks: query => ({
		type: 'accountAssets/getFeatureRanks',
		payload: query || {},
	}),
};

@connect(mapStateToProps, mapDispatchToProps)
class AnalysisReport extends Component {
	static propTypes = {
		detail: PropTypes.object.isRequired,
	}
	state = {
		specialTradings: undefined,
		commentConfig: undefined,
		featureRanks: undefined,
	}
	componentDidMount() {
		this.getData(this.props);
	}
	componentWillReceiveProps(nextProps) {
		if (nextProps !== this.props && nextProps.detail !== this.props.detail) {
			this.getData(nextProps);
		}
	}
	getData({ detail: { trader, dataDate }, getSpecialTradings, getCommentConfig, getFeatureRanks }) {
		getSpecialTradings({ trader });
		getCommentConfig();
		getFeatureRanks({ trader, dataDate, featureNameList:['fdtScore'] });
	}
	render() {
		const { detail, specialTradings, commentConfig,  featureRanks} = this.props;
		const fdtFeatures = {};
		for (const k in detail) {
			const value = detail[k];
			fdtFeatures[utils.toSnakeCase(k)] = value;
		}
		return (
			<div className="analysisReport">
				{ fdtFeatures && !_.isEmpty(commentConfig) && !_.isEmpty(featureRanks) && 
					<Comments features={fdtFeatures} config={commentConfig} special_tradings={specialTradings} feature_ranks={featureRanks} />
				}
				<Row>
					<dl>
						<dt className="markdown-body"><ReactMarkdown source="#### 案例分析:" /></dt>
						<dd>
							{specialTradings && specialTradings.map(((item, i) => {
								const { closeOrder, openOrderId, closeOrderId } = item;
								if (_.isUndefined(closeOrder.closedPositionList) || _.isEmpty(closeOrder.closedPositionList)) {
									return <span key={i} />;
								}
								return (
									<div key={i} style={{ marginTop: 10 }}>
										<strong>{item.specialText}</strong>
										<OrderDetail data={closeOrder} openOrderId={openOrderId} closeOrderId={closeOrderId} closeOrder={closeOrder}/>
									</div>
								);
							}))}
						</dd>
					</dl>
				</Row>
			</div>
		);
	}
}

export default AnalysisReport;
